app.controller("countDetailCtrl", ["$scope",'datepickerPopupConfig', 'datepickerConfig',"CountApi","$timeout","sweetAlertUtil",
    function($scope,datepickerPopupConfig, datepickerConfig,CountApi,$timeout,sweetAlertUtil) {
    	$scope.result = []
        $scope.AREA = "AREA"
  		$scope.vm={
  			year:""
  		}
        $scope.year=''

        // ---------------------start 初始化城市 -------------------------------
        function initCity(){
            CountApi.getCity().then(function(res){
                if(res.code == 200){
                    $scope.city = res.result.list
                    $timeout(function(){
                        $scope.item = "C51"
                    })
                    $timeout(function(){
                        $scope.item_1 = "C510100000000"
                    })
                    $timeout(function(){
                        $scope.item_2 = 'C510101000000'
                    })
                    $timeout(function(){
                        init_authority()
                    })
                    

                }
            },function(){})
        }
        $scope.$watch('item',function(value){
            if($scope.item == undefined){
                return
            }
            CountApi.getCity({parentId:$scope.item}).then(function(res){
                if(res.code == 200){
                    $scope.city_1=res.result.list
                }
            },function(){})
            $scope.item_1=null;
            $scope.item_2=null;

        });
        $scope.$watch('item_1',function(value){
            if($scope.item_1 == undefined){
                return
            }
            CountApi.getCity({parentId:$scope.item_1}).then(function(res){
                if(res.code == 200){
                    $scope.city_2=res.result.list
                }
            },function(){})
            $scope.item_2=null;
        });
        // ---------------------end 初始化城市 -------------------------------

      
        //------------------------start 初始化选择时间组件 -------------------------------
        initDatePicker()
        function initDatePicker() {
            datepickerConfig.minMode = "year"
            datepickerConfig.datepickerMode="year"
            datepickerConfig.showWeeks = false;
            datepickerPopupConfig.showButtonBar = false

            Date.prototype.format = function(format) {
                var o = {
                    "M+": this.getMonth() + 1, //month 
                    "d+": this.getDate(), //day 
                    "h+": this.getHours(), //hour 
                    "m+": this.getMinutes(), //minute 
                    "s+": this.getSeconds(), //second 
                    "q+": Math.floor((this.getMonth() + 3) / 3), //quarter 
                    "S": this.getMilliseconds() //millisecond 
                }

                if (/(y+)/.test(format)) {
                    format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
                }

                for (var k in o) {
                    if (new RegExp("(" + k + ")").test(format)) {
                        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
                    }
                }
                return format;
            }
        }
        //------------------------end 初始化选择时间组件 -------------------------------
        

        //------------------------start 初始化统计明细 -------------------------------
        

        //  初始化角色 
        
        function initRole(){
            CountApi.getArea().then(function(res){
                if(res.code == 200){
                   $scope.areaCity = res.result.citys
                   $scope.item_3 = res.result.citys[0].city_id
                   init_authority()
                }
            },function(){})            
        }
       

        init()
        function init(){
            CountApi.getUserInfo().then(function(data){
                if(data.code == 200){
                    // 判断用户
                  if(data.result.role_tag == 'AREA'){
                    $scope.AREA = "AREA"
                    initRole()
                  }else{
                    $scope.AREA = "ROOT"
                    initCity()

                  }
                    
                }
            },function(){

            }) 
        }

        function init_authority(){
            if($scope.AREA == "AREA"){
                if($scope.item_3==undefined || $scope.item_3==null){
                  sweetAlertUtil.fail("必须选择区域！");
                  return
                }
                var params = {
                    city_id:$scope.item_3,
                    year:$scope.year
                }
            }else{
                if($scope.item_2==undefined || $scope.item_2==null){
                  sweetAlertUtil.fail("必须选择区域！");
                  return
                }
                var params = {
                    city_id:$scope.item_2,
                    year:$scope.year
                }   
            }
            CountApi.getCountData(params).then(function(data){
              if(data.code == 200){
                $scope.result  = data.result
              }
            },function(){})
        }
        //------------------------end 初始化统计明细 -------------------------------

        //------------------------start 搜索 -------------------------------

        $scope.searchByDateRange = function($event){
            $scope.year = $scope.vm.year!='' ? $scope.vm.year.format("yyyy") : '';            
            init_authority()
        }
        //------------------------end 搜索 -------------------------------

    }
]);
